home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Workspace / TickleServices / Documentation / E.Aux / performService.m < prev    next >
Encoding:
Text File  |  1994-03-22  |  2.4 KB  |  100 lines

  1. /* This command was contributed by Mike Dixon <mdixon@parc.xerox.com>
  2.  * cc -o performService -s -O performService.m -lNeXT_s
  3.  * cc -arch m68k -arch i386 -o performService -s -O performService.m -lNeXT_s
  4.  */
  5. #import <stdlib.h>
  6. #import <stdio.h>
  7. #import <libc.h>
  8. #import <strings.h>
  9. #import <appkit/appkit.h>
  10.  
  11. #define STDIN 0
  12. #define STDOUT 1
  13. #define STDERR 2
  14.  
  15. char *use =
  16. "Use: performService <service> {-stdin | -input 'foo' | -empty} <inType> <outType>*\n";
  17.  
  18. id pb = 0;
  19.  
  20. void die (char *msg)
  21. {
  22.     write(STDERR, msg, strlen(msg));
  23.     if (pb)
  24.     [pb freeGlobally];
  25.     exit(-1);
  26. }
  27.  
  28. void main (int argc, char **argv) {
  29.     const char *serviceName, *sendTypes[2], **recvTypes, *const *rcvdTypes, *const *p;
  30.     char *sendBuf, *recvBuf;
  31.     int nextArg, allocSize, bytesRead, sendLen, numRecvType, recvLen, i;
  32.  
  33.     if (argc < 4)
  34.     die(use);
  35.  
  36.     serviceName = argv[1];
  37.     nextArg = 2;
  38.  
  39.     if (! strcmp(argv[nextArg], "-stdin")) {
  40.     allocSize = 15000;
  41.     sendBuf = malloc(allocSize);
  42.     sendLen = bytesRead = read(STDIN, sendBuf, allocSize);
  43.     while (bytesRead>0) {
  44.         allocSize += allocSize;
  45.         sendBuf = realloc(sendBuf, sendLen + allocSize);
  46.         bytesRead = read(STDIN, sendBuf+sendLen, allocSize);
  47.         sendLen += bytesRead;
  48.     }
  49.     ++nextArg;
  50.     } else if (! strcmp(argv[nextArg], "-input")) {
  51.     sendBuf = argv[nextArg+1];
  52.     sendLen = strlen(sendBuf);
  53.     nextArg += 2;
  54.     } else if (! strcmp(argv[nextArg], "-empty")) {
  55.     sendBuf = "";
  56.     sendLen = 0;
  57.     ++nextArg;
  58.     } else
  59.     die(use);
  60.  
  61.     if (argc <= nextArg)
  62.     die(use);
  63.  
  64.     sendTypes[0] = argv[nextArg];
  65.     sendTypes[1] = 0;
  66.     ++nextArg;
  67.  
  68.     numRecvType = argc - nextArg;
  69.     recvTypes = &argv[nextArg];
  70.  
  71.     pb = [Pasteboard newUnique];
  72.     NXApp=[Application new];
  73.     [pb declareTypes: sendTypes num: 1 owner: NULL];
  74.  
  75.     if (! [pb writeType: sendTypes[0] data: sendBuf length: sendLen])
  76.     die("Couldn't write my pasteboard.\n");
  77.  
  78.     if (! NXPerformService(serviceName, pb))
  79.     die("No such service.\n");
  80.     
  81.     if (numRecvType == 0) {
  82.     sleep( 10);
  83.     [pb freeGlobally];
  84.     exit(0);
  85.     }
  86.  
  87.     rcvdTypes = [pb types];
  88.     for (i = 0; i < numRecvType; i++)
  89.     for (p = rcvdTypes; *p; p++)
  90.         if (! strcmp(recvTypes[i], *p)) {
  91.         if ([pb readType: *p data: &recvBuf length: &recvLen]) {
  92.             write(STDOUT, recvBuf, recvLen);
  93.             [pb freeGlobally];
  94.             exit(0);
  95.         }
  96.         die("Error reading service reply.\n");
  97.         }
  98.     die("No reply of the requested type received from service.\n");
  99. }
  100.